﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using space_matters.Utilities;
using System;

namespace space_matters
{
    class Planet : Matter
    {
        #region Private Elements

        public PlanetType _type {get; set; }
        public Random random = new Random();

        #endregion

        #region Type Definitions

        public enum PlanetType
        {
            RedPlanet,
            EarthPlanet,
            GasPlanet,
            IcePlanet,
            BluePlanet,
        }

        #endregion

        public Texture2D ChooseTexture(PlanetType type)
        {
            switch (type)
            {
                case PlanetType.RedPlanet:
                    return Assets.Get<Texture2D>("Red Planet");

                case PlanetType.EarthPlanet:
                    return Assets.Get<Texture2D>("Earth Planet");

                case PlanetType.GasPlanet:
                    return Assets.Get<Texture2D>("Gas Planet");

                case PlanetType.IcePlanet:
                    return Assets.Get<Texture2D>("Ice Planet");

                case PlanetType.BluePlanet:
                    return Assets.Get<Texture2D>("Blue Planet");
            }

            return null;
        }

        #region Constructors

        public Planet(PlanetType type, double mass, double radius, Vector2 position) : this(type, mass, radius, position, Vector2.Zero)
        {
            _type = type;
        }

        public Planet(PlanetType type, double mass, double radius, Vector2 position, Vector2 velocity, float maxVelocity = float.PositiveInfinity) : base(Color.White, mass, radius, position, velocity, maxVelocity)
        {
            TextureOverlayOpacity = 1;

            _type = type;

            Texture = ChooseTexture(type);

            Animation = new Animation(48, 0.02, 50, 50, 12, 4, 0, 0, 1);
            Texture = Assets.Get<Texture2D>("Forming");
        }

        #endregion

        public override void Draw(SpriteBatch spriteBatch, GameTime time)
        {
            if (Animation.IsFinished())
            {
                Animation = new Animation(48, 0.08, 50, 50, 12, 4, 0, 0);
                Texture = ChooseTexture(_type);

                if (_type == PlanetType.EarthPlanet)
                {
                    TextureOverlay = Assets.Get<Texture2D>("Earth Planet Glow");

                    Game.GetGame().FadeTime = 5;
                    Game.GetGame().TotalFadeTime = 5;

                    var planetSound = Assets.Get<SoundEffect>("Fanfare");
                    planetSound.Play();
                }
                else
                {
                    var planetSound = Assets.Get<SoundEffect>("Making Planet");
                    planetSound.Play();
                }
            }

            base.Draw(spriteBatch, time);
        }
    }
}
